// pages/history/history.js
const app = getApp();
Page({
  data: {
    bgTextStyle: 'dark',
    scrollTop: '200rpx',
    bgColor: '#ff0000',
    bgColorTop: '#00ff00',
    bgColorBottom: '#0000ff',
    nbTitle: '缴费记录',
    nbLoading: false,
    nbFrontColor: '#000000',
    nbBackgroundColor: '#ffffff',
    pageStyle: "color:#333333;background-color: #f1f1f1;",
    navs:[
      {title: "首页",class:"", path:"/pages/index/index", icon: "", font: "fa fa-home"},
      {title: "信息",class:"", path:"", icon: "", font: "fa fa-comments-o"},
      {title: "维修",class:"", path:"", icon: "", font: "fa fa-wrench"},
      {title: "我的",class:"co", path:"/pages/mine/mine", icon: "", font: "fa fa-user-circle"},
    ],
    userInfo: {
      nickName: "",
      addr: "",
      phone: "",
      avatar: ""
    },
    orderList:[],
    hasChange: false,
    ready: false
  },
  onLoad: function (options) {
  },
  onReady: function () {
  },
  onShow: function () {
    let v = this;
    if (app.globalData.token){
      v.getHistory();
    } else {
      console.log("app.doLogin");
      app.doLogin(v,v.getHistory);
    }
  },

  getHistory(){
    let v = this;
    v.setData({hasChange: false});
    let url = app.globalData.host + '/api/user/order_list';
    wx.request({
      url: url, data: {token: app.globalData.token},
      success (res) {
        let r = res.data;
        if(r.code == 200 && r.data) {
          let l = r.data.list || [];
          console.log("getHistory",l);
          v.setData({orderList: l});
        }
      }
    })
  },
  navTap(ev){
    let v = this;
    let ds = ev.currentTarget.dataset;
    let ll = v.data.navs;
    if(ds && ll[ds.idx]) {
      let lk = ll[ds.idx];
      let path = '/';
      if(lk){
        if(lk.path) {
          path = lk.path ;
          wx.reLaunch({ url: path});
        }
      }
      console.log('navTap',path, lk);
    }
  },
  requestTicket(ev){
    let v = this;
    let ds = ev.target.dataset;
    if(!ds || !ds.id) return;
    let order_id = ds.id;
    console.log("requestTicket", order_id);
    app.globalData.order_id = order_id;
    let ll = v.data.orderList;
    for(let i = 0; i < ll.length; i++) {
      if(ll[i].order_id == order_id) {
        app.globalData.orderInfo = ll[i];
      }
    }
    wx.navigateTo({ url: "/pages/apply/apply"});
    
    // wx.chooseInvoiceTitle({ success(res) {} });
  },
  onSuccess: function (res) {
    console.log(res.detail);
  },
  onFail: function (res) {
    console.log(res);
  },
  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },

  onShareAppMessage: function () {
  }
})